/**
 * 
 */
package com.cn.practice.factory;

/**
 * 简单工厂练习-温州造鞋
 * 能生产皮鞋 拖鞋 工厂放入不同的材料得出不同的鞋
 * @author zhangyang
 *
 */
public class SimpleFactoryPractice {
public static void main(String[] args) {
	shoesSimpleFactory simpleFactory = new shoesSimpleFactory();
	Shoes shoes = simpleFactory.productShoes("皮");
	System.out.println(shoes);
	ShoesFactoryMethodImpl factoryMethodImpl = new ShoesFactoryMethodImpl();
	Shoes shoes2 = factoryMethodImpl.productShoes("皮");
	System.out.println(shoes2);
	ShoesFactoryImpl factoryImpl = new ShoesFactoryImpl();
	Shoes shoes3 = factoryImpl.productShoes("皮");
	System.out.println(shoes3);
}
}

abstract class Shoes{
	private String name;
	private String material;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMaterial() {
		return material;
	}
	public void setMaterial(String material) {
		this.material = material;
	}
	
	
	/**
	 * 生产鞋子的方法
	 */
	abstract Shoes product();
	@Override
	public String toString() {
		return "Shoes [name=" + name + ", material=" + material + "]";
	}
	
}
class LeatherShoes extends Shoes{

	@Override
	Shoes product() {
		// TODO Auto-generated method stub
		this.setName("皮鞋");
		this.setMaterial("皮");
		System.out.println("开始生产皮鞋");
		return this;
	}
	
}
/**
 * 简单鞋子工厂 生产鞋子
 * 根据不同的原料生产不同的鞋子
 * @author zhangyang
 *
 */
class shoesSimpleFactory{
	Shoes shoes;
	public Shoes productShoes(String material) {
		if("皮".equals(material)) {
			LeatherShoes shoes2 = new LeatherShoes();
			shoes=shoes2.product();
		}
		return shoes;
	}
}

/**
 * 工厂方法-简单工厂的升级版 将工厂生产方法定义为抽象
 * 交给具体实现类实现生产
 * @author zhangyang
 *
 */
abstract class ShoesFactoryMethod{
	public abstract Shoes productShoes(String material);
}

class ShoesFactoryMethodImpl extends ShoesFactoryMethod{

	@Override
	public Shoes productShoes(String material) {
		// TODO Auto-generated method stub
		Shoes shoes=null;
		if("皮".equals(material)) {
			LeatherShoes shoes2 = new LeatherShoes();
			shoes=shoes2.product();
		}
		return shoes;
	}

/**
 * 抽象工厂是工厂方法的升级 将工厂方法由抽象类变为接口 
 * 这样子类只需要实现方法就可以了，并且解决了单继承问题
 * 子类也能是一个抽象类 继续往下生成工厂
 */
}
interface ShoesFactory{
	public Shoes productShoes(String material);
}
class ShoesFactoryImpl implements ShoesFactory{

	@Override
	public Shoes productShoes(String material) {
		// TODO Auto-generated method stub
		Shoes shoes=null;
		if("皮".equals(material)) {
			LeatherShoes shoes2 = new LeatherShoes();
			shoes=shoes2.product();
		}
		return shoes;
	}
	
}